#include "StdAfx.h"
#include "OnRailMoveAction.h"


OnRailMoveAction::OnRailMoveAction(std::vector<Ogre::Vector3*> *r)
{
	rail = r;
}


OnRailMoveAction::~OnRailMoveAction(void)
{
}

bool OnRailMoveAction::FireTrigger(EntityControl *entity)
{
	int positionCounter = ((IntEntityParameter*)entity->getParameter(MAP_WAYPOINT_PARAMETER))->getIntValue();
	
	if(positionCounter<(rail->size()))
	{
		Ogre::Vector3 finalPosition = *(rail->at(positionCounter));

		if(positionCounter>0)
		{
			Ogre::Vector3 * position =rail->at(positionCounter);
			Ogre::Vector3 * prevPosition = rail->at(positionCounter-1);
			Ogre::Vector3  posDiff= (*position) -(*prevPosition);

			Ogre::Vector3 *entityPos = (Ogre::Vector3*)entity->getParameter(POSITION_PARAMETER)->getValue();
			finalPosition = *entityPos+posDiff;

			/*if((*position-finalPosition).length() >250)
			{
				Ogre::Vector3 dist  =(*position-finalPosition);
				dist.normalise();
				dist*=200;
				finalPosition =   *position +dist;
			}*/
		}



		
		if(positionCounter+75<(rail->size()))
		{
			entity->setPosition(finalPosition);
			entity->setLookAt(*(rail->at(positionCounter+15)));

			positionCounter++;

			((IntEntityParameter*)entity->getParameter(MAP_WAYPOINT_PARAMETER))->setIntValue(positionCounter);

		}

	}

	return true;

}
